<template>
  <van-swipe class="my-swipe" :autoplay="3000" indicator-color="black">
    <van-swipe-item v-for="item in state.slideshow" :key="item.id">
      <img :src="item.img" alt="" class="slideshow">
      <span>{{ item.text }}</span>
    </van-swipe-item>
    <template #indicator="{ active, total }">
      <div class="custom-indicator">
        <ul>
          <li v-for="item in total" :key="item" :class="{ show: item === active+1 }"></li>
        </ul>
      </div>
    </template>
  </van-swipe>

  <div class="videos">
    <div class="video" v-for="item in state.videos" :key="item.id">
      <img :src="item.img" alt="" class="img">
      <div class="text">{{ item.title }}</div>
      <div class="author">{{ item.author }}</div>
      <div class="number">
        <span>{{ item.number }}</span>
        <span>{{ item.danmu }}</span>
        <span>{{ item.time }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onBeforeMount, reactive } from 'vue';
import axios from '../../api/index.js'
import { onMounted } from 'vue';
const state = reactive({
  videos: [],
  slideshow: []
})


onBeforeMount(
  async () => {
    const res = await axios.post('/recommend')
    state.videos = res.data
  }
)
onMounted(
  async () => {
    const res = await axios.post('/slideshow')
    state.slideshow = res.data
  }
)
</script>

<style lang="less" scoped>
.my-swipe {
  position: relative;
}

.my-swipe .van-swipe-item {
  display: flex;
  justify-content: center;
  color: #fff;

  img {
    width: 96%;
    border-radius: 0.2rem;
  }

  span {
    position: absolute;
    bottom: 0.15rem;
    left: 0.4rem;
    font-size: 0.4rem;
    font-weight: bold;
  }

}

.custom-indicator {
  width: 0.2rem;
  width: 2rem;
  position: absolute;
  bottom: 0.2rem;
  right: 0.2rem;
  ul {
    display: flex;
    li {
      margin: 0 0.1rem;
      width: 0.15rem;
      height: 0.15rem;
      border-radius: 50%;
      background: #aaa;
      box-sizing: border-box;
    }
    .show{
      background: #fff;
    }
  }
}

.videos {
  display: flex;
  flex-wrap: wrap;
}

.video {
  width: 4.8rem;
  height: 4.6rem;
  border-radius: 10px;
  border: 0.1rem solid;
  border-color: transparent #eee #eee transparent;
  position: relative;
}

.img {
  width: 100%;
  height: 2.8rem;
}

.text {
  margin: 0.1rem 0 0.3rem 0;
  padding: 0 0.2rem;
  box-sizing: border-box;
  width: 100%;
  height: 1rem;
  font-size: 0.35rem;
  line-height: 0.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  word-break: break-all;
  -webkit-box-orient: vertical;
}

.author {
  width: 100%;
  height: 0.6rem;
  font-size: 0.1rem;
  position: absolute;
  bottom: 0.1rem;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  color: #999;
  transform: scale(0.9);
}

.number {
  width: 100%;
  position: absolute;
  bottom: 1.8rem;
  font-size: 0.3rem;
  display: flex;
  justify-content: space-around;
  color: #fff;

  span {
    margin-right: 0.1rem;
  }
}

.slideshow {
  width: 10rem;
  height: 5rem;
}</style>